/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.model.core;

import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.xml.XMLUtility;

//import org.eclipse.core.runtime.IAdaptable;

/**
 * @author zl
 * @author Zhao Yi
 *
 */
public class MetaNamedElement extends MetaModelElement{
	protected static final String NAME_DEFAULT = "";
	
	
	protected String name = NAME_DEFAULT;

	@MethodProperty(
			id = "NAMEDELEMENT_NAME",
			catalog = "Model",
			name = CorePackage.NAMEDELEMENT_NAME,
			setter = "setName"
			)
	public String getName() {
		return name;
	}
	
	public String getNamespace() {
		MetaElement ele = this.getContainer();
		String namespace = "";
//		if(ele instanceof MetaClass) {
//			namespace = "." + ((MetaClass)ele).getName();
//			ele = ele.getContainer();
//		}
//		else if(ele instanceof MetaEnum) {
//			namespace = "." + ((MetaEnum)ele).getName();
//			ele = ele.getContainer();
//		}
		while(ele instanceof MetaNamedElement) {
			namespace = "." + ((MetaNamedElement)ele).getName() + namespace;
			ele = ele.getContainer();
		}
		if(namespace.startsWith(".")) {
			namespace = namespace.substring(1);
		}
		return namespace;
	}
	
	public String getFullName() {
		return getNamespace() + "." + name;
	}

	public void setName(String newName) {
		String oldName = name;
		name = newName;
		firePropertyChange(CorePackage.NAMEDELEMENT_NAME, oldName, newName);
	}
	
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeAttribute(root, CorePackage.NAMEDELEMENT_NAME, name);
	}
	
	public void deSerialize(Element root) {
		super.deSerialize(root);
		name = XMLUtility.xmlInstance.deSerializeString(root, CorePackage.NAMEDELEMENT_NAME);
	}
	
	public String toString() {
		return name;
	}
	
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EMetaModelPackage.EMETA_NAMED_ELEMENT__NAME:
				return getName();
		}
		return super.eGet(featureID, resolve, coreType);
	}
}
